// newHousePackage/pages/incomeList/incomeList.js
import { myIncomeList } from "../../../../service/agency_service"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [

    ],
    scrollRefreshStatus: true,
    page: 0,
    pageSize: 10,
    isShowLoading: false,
    isBottomShow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.myIncomeList();
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  myIncomeList() {
    wx.showLoading();
    let pageNum = this.data.page;
    let pageSize = this.data.pageSize;
    myIncomeList(pageNum, pageSize).then(res => {
      wx.hideLoading();
      this.setData({
        scrollRefreshStatus: false,
      })
      console.log("JJLog:收益列表:" + JSON.stringify(res));
      if (res.code == 200) {
        let tempArr = [];
        tempArr = this.data.dataList;
        if (tempArr.length > 0 && pageNum > 0) {
          //加载下一页:
          tempArr = tempArr.concat(res.data);
          if (res.data.length < pageSize) {
            this.setData({
              isShowLoading: false,
              isBottomShow: true,
            })
            this.setData({
              dataList: tempArr
            })
          } else if (res.data.length == pageSize) {
            this.setData({
              isShowLoading: false,
            })
            this.setData({
              dataList: tempArr
            })
          }
        } else {
          this.setData({
            isShowLoading: false,
          })
          this.setData({
            dataList: res.data
          })
          if (res.data.length < pageSize) {
            this.setData({
              isBottomShow: true
            })
          }
        }
      }
    }).catch(err => {
      wx.hideLoading();
      this.setData({
        scrollRefreshStatus: false,
      })

      let page = this.data.page;
      if (page > 0) {
        page = page - 1;
      }
      this.setData({
        isShowLoading: false,
        page: page
      })
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:myIncomeList", err);
    })
  },

  //滚动到底了
  scrolltolower() {
    if (this.data.isBottomShow) {
      //已经到底了就不加载了
      return;
    }
    let tempPage = this.data.page + 1;
    this.setData({
      page: tempPage,
      isShowLoading: true,
    })
    this.myIncomeList();
  },
  //下拉刷新:
  refreshDownData() {
    this.setData({
      page: 0,
      isBottomShow: false
    })
    this.myIncomeList();
  },

})